<?php 
$this->load->view('admin/layout/head', array('title'=>'地区设置'), FALSE);
?>

<div id="container">
	<div class="panel panel-default region-panel region-panel-1">
		<div class="panel-heading">
		    <h3 class="panel-title">英国</h3>
		    <div class="menu">
		    	<a href="javascript:;" class="add-btn" pid="1" type="1">添加地区</a>
		    </div>
		</div>
		<div class="panel-body">
			<?php foreach($regions as $r): ?>
			<?php if($r['type'] == 1): ?>
				<div rid="<?php echo $r['id'] ?>">
					<a href="javascript:;" rid="<?php echo $r['id'] ?>" class="c-btn">
					<em><?php echo $r['name'] ?></em>
					<i><?php echo $r['en_name'] ?></i>
					</a>
					<a href="javascript:;" class="modify-btn">修改</a>
					<a href="javascript:;" class="del-btn">删除</a>
				</div>
			<?php endif; ?>
			<?php endforeach; ?>
		</div>
	</div>
	<div class="panel panel-default region-panel region-panel-2" style="display: none">
		<div class="panel-heading">
		</div>
		<div class="panel-body">
			
		</div>
	</div>
	<div class="panel panel-default region-panel region-panel-3" style="display: none">
		<div class="panel-heading">
		</div>
		<div class="panel-body">
			
		</div>
	</div>
</div>


<div id="form-div" style="display: none">
	<form id="region-form" class="form-horizontal">
		<input type="hidden" name="id" value="0">
		<input type="hidden" name="parent_id" value="0">
		<input type="hidden" name="type" value="0">
		<div class="form-group row">	   
		    <label class="col-sm-3 control-label">中文名称：</label>
		    <div class="col-sm-6">
		    	<input type="text" name="name" value="" class="form-control" placeholder="请输入中文地区名称">
		    </div>
		</div>
		<div class="form-group row">	   
		    <label class="col-sm-3 control-label">英文名称：</label>
		    <div class="col-sm-6">
		    	<input type="text" name="en_name" value="" class="form-control" placeholder="请输入英文地区名称">
		    </div>
		</div>
	</form>
</div>


<?php 
$this->load->view('admin/layout/footer');
?>

<script type="text/javascript">
	var dlg = null;
	$(function(){
		var form_html = $('#form-div').html();
		$('#form-div').remove();

		$('body').on('click','.panel-body a.c-btn',function(){
			var rid = $(this).attr('rid');
			$.get('/admin/region',{id:rid} ,function(data) {
				var json = data;
				var html = '<h3 class="panel-title">'+json.region.name+'</h3><div class="menu"><a href="javascript:;" class="add-btn" pid="'+json.region.id+'" type="'+(parseInt(json.region.type)+1)+'">添加地区</a>';
				var ele = null;
				if(json.region.type == 1){
					$('.region-panel-3').hide();
					ele = $('.region-panel-2');
				}else{
					ele = $('.region-panel-3');
				}

				var aHtml = '';
				for(var i=0; i < json.regions.length; i++){
					if(json.region.type == 1){
						aHtml += '<div rid="'+json.regions[i].id+'"><a href="javascript:;" rid="'+json.regions[i].id+'" class="c-btn"><em>'+json.regions[i].name+'</em><i>'+json.regions[i].en_name+'</i></a><a href="javascript:;" class="modify-btn">修改</a><a href="javascript:;" class="del-btn">删除</a></div>'
					}else{
						aHtml += '<div rid="'+json.regions[i].id+'"><span class="c-btn"><em>'+json.regions[i].name+'</em><i>'+json.regions[i].en_name+'</i></span><a href="javascript:;" class="modify-btn">修改</a><a href="javascript:;" class="del-btn">删除</a></div>'
					}
					
				}
				ele.find('.panel-heading').html(html);
				ele.find('.panel-body').html(aHtml);
				ele.show();
			});
		})

		//添加
		$('body').on('click','.add-btn',function(){
			var btn = '<a href="javascript:;" class="btn btn-primary form-submit">提交</a>';
			dlg = dialog('添加地区',form_html,btn);
			$('#region-form input[name=parent_id]').val($(this).attr('pid'));
			$('#region-form input[name=type]').val($(this).attr('type'));
			$('.form-submit').on('click',form_submit);
		});

		//修改
		$('body').on('click','.modify-btn',function(){
			var btn = '<a href="javascript:;" class="btn btn-primary form-submit">提交</a>';
			dlg = dialog('修改地区',form_html,btn);
			var id = $(this).parent().attr('rid');
			var name = $(this).parent().find('.c-btn em').text();
			var en_name = $(this).parent().find('.c-btn i').text();

			$('#region-form input[name=id]').val(id);
			$('#region-form input[name=name]').val(name);
			$('#region-form input[name=en_name]').val(en_name);
			$('.form-submit').on('click',form_submit);
		});

		//删除
		$('body').on('click','.del-btn',function(){
			var id = $(this).parent().attr('rid');
			var that = $(this);
			var clg = confirm('确定要删除此地区吗？',function(e){
				if(e.index == 1){
					$.post('/admin/region/delete', {id}, function(data, textStatus, xhr) {
						clg.modal('hide');
						that.parent().remove();
						if($('.add-btn[pid='+id+']').length>0){
							$('.add-btn[pid='+id+']').parents('.panel').hide();
							$('.add-btn[pid='+id+']').parents('.panel').next().hide();
						}
						Toast.success('删除成功');
					});
				}
			})
			
		})
		
	})

	function form_submit(){
		$.post('/admin/region/save', $('#region-form').serialize(), function(json, textStatus, xhr) {
			json = $.parseJSON(json);
			if(json.code == 1){
				alert(json.msg);
				return;
			}
			console.log(json);
			var name = $('#region-form input[name=name]').val();
			var en_name = $('#region-form input[name=en_name]').val();
			var parent_id = $('#region-form input[name=parent_id]').val();
			if($('#region-form input[name=id]').val()>0){
				var rid = $('#region-form input[name=id]').val();
				$('.panel-body div[rid='+rid+'] .c-btn em').text(name);
				$('.panel-body div[rid='+rid+'] .c-btn i').text(en_name);
			}else{
				var pbody = $('.add-btn[pid='+parent_id+']').parents('.panel').find('.panel-body');
				var html = '';
				if(pbody.hasClass('.region-panel-3')){
					html = '<div rid="'+json.data.id+'"><span class="c-btn"><em>'+json.data.name+'</em><i>'+json.data.en_name+'</i></a><a href="javascript:;" class="modify-btn">修改</a><a href="javascript:;" class="del-btn">删除</span></div>'
				}else{
					html = '<div rid="'+json.data.id+'"><a href="javascript:;" rid="'+json.data.id+'" class="c-btn"><em>'+json.data.name+'</em><i>'+json.data.en_name+'</i></a><a href="javascript:;" class="modify-btn">修改</a><a href="javascript:;" class="del-btn">删除</a></div>'
				}
				pbody.append(html);
				
			}
			Toast.success('操作成功',function(){
				dlg.modal('hide');
			})
			
		});
	}
</script>
















